/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alice.sentinel.biz.slots.flow;

import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.node.DefaultNode;

import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleChecker;
import com.alibaba.csp.sentinel.slots.logger.EagleEyeLogUtil;
import com.alibaba.fastjson.JSONObject;
import com.alice.sentinel.biz.constants.Constants;
import com.alice.sentinel.biz.slotchain.AbstractLinkedProcessorSlot;
import com.alice.sentinel.biz.slotchain.ResourceWrapper;
import com.alice.sentinel.biz.spi.Spi;
import com.alice.sentinel.biz.util.AssertUtil;
import com.alice.sentinel.biz.util.function.Function;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import com.alice.sentinel.biz.context.Context;
import lombok.extern.slf4j.Slf4j;


@Spi(order = Constants.ORDER_FLOW_SLOT)
@Slf4j
public class FlowSlot extends AbstractLinkedProcessorSlot<DefaultNode> {

    //private final FlowRuleChecker checker;

    public FlowSlot() {
        //this(new FlowRuleChecker());
    }

    /**
     * Package-private for test.
     *
     * @param  flow rule checker
     * @since 1.6.1
     */
 /*   FlowSlot(FlowRuleChecker checker) {
        AssertUtil.notNull(checker, "flow checker should not be null");
        this.checker = checker;
    }*/

    //具体实现
    @Override
    public void entry(Context context, ResourceWrapper resourceWrapper, DefaultNode node, int count,
                      boolean prioritized, Object... args) throws Throwable {

        log.info("FlowSlot entry ");
        //业务处理
        checkFlow(resourceWrapper, context, node, count, prioritized);

        //下一个执行
        fireEntry(context, resourceWrapper, node, count, prioritized, args);
    }

    void checkFlow(ResourceWrapper resource, Context context, DefaultNode node, int count, boolean prioritized)
        throws BlockException {
        Collection<FlowRule> rules = ruleProvider.apply(resource.getName());
        if (rules != null) {
            //todo
        }
        System.out.println(JSONObject.toJSON(rules));

    }

    @Override
    public void exit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {
        log.info("FlowSlot exit ");
        fireExit(context, resourceWrapper, count, args);
    }

    private final Function<String, Collection<FlowRule>> ruleProvider = new Function<String, Collection<FlowRule>>() {
        @Override
        public Collection<FlowRule> apply(String resource) {
            // Flow rule map should not be null.
            Map<String, List<FlowRule>> flowRules = FlowRuleManager.getFlowRuleMap();
            return flowRules.get(resource);
        }
    };


}
